# Contributor: kpcyrd <git@rxv.cc>
# Maintainer: kpcyrd <git@rxv.cc>
pkgname=rebuilderd
pkgver=0.19.0
pkgrel=3
pkgdesc="Independent verification system of binary packages"
url='https://github.com/kpcyrd/rebuilderd'
arch="all !s390x !ppc64le !riscv64"
license="GPL-3.0-or-later"
install="$pkgname.pre-install"
pkgusers="rebuilderd"
pkggroups="rebuilderd"
depends="
	$pkgname-tools
	shared-mime-info
	"
makedepends="
	cargo
	cargo-auditable
	openssl-dev>3
	scdoc
	sqlite-dev
	xz-dev
	zstd-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	$pkgname-tools
	"
options="net"
source="$pkgname-$pkgver.tar.gz::https://github.com/kpcyrd/rebuilderd/archive/v$pkgver.tar.gz
	rebuilderd.initd
	rebuilderd-worker.initd
	rebuilderd.confd
	rebuilderd-sync.cron

	getrandom-0.2.10.patch
	shared-mime-info-2.3.patch
	"


prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --release --frozen

	mkdir -p completions
	target/release/rebuildctl completions bash > completions/rebuildctl.bash
	target/release/rebuildctl completions zsh > completions/_rebuildctl
	target/release/rebuildctl completions fish > completions/rebuildctl.fish

	make docs
}

check() {
	cargo test --release --frozen
}

package() {
	install -Dm755 \
		"$builddir/target/release/rebuilderd" \
		"$builddir/target/release/rebuilderd-worker" \
		-t "$pkgdir/usr/bin"

	# install rebuilder scripts
	install -Dm 755 -t "$pkgdir/usr/libexec/rebuilderd" \
		worker/rebuilder-*.sh

	# install config files
	install -Dm 644 -t "$pkgdir/etc" \
		contrib/confs/rebuilderd-sync.conf
	install -Dm 640 -g rebuilderd -t "$pkgdir/etc" \
		contrib/confs/rebuilderd-worker.conf \
		contrib/confs/rebuilderd.conf

	# init scripts
	install -Dm755 "$srcdir/$pkgname.initd" \
		"$pkgdir/etc/init.d/$pkgname"
	install -Dm755 "$srcdir/$pkgname-worker.initd" \
		"$pkgdir/etc/init.d/$pkgname-worker"
	install -Dm644 "$srcdir/$pkgname.confd" \
		"$pkgdir/etc/conf.d/$pkgname"
	install -Dm755 "$srcdir/rebuilderd-sync.cron" \
		"$pkgdir"/etc/periodic/hourly/rebuilderd-sync

	# install docs
	install -Dm 644 -t "$pkgdir/usr/share/doc/rebuilderd" README.md
	install -Dm 644 -t "$pkgdir/usr/share/man/man1" \
		contrib/docs/rebuilderd.1 \
		contrib/docs/rebuildctl.1 \
		contrib/docs/rebuilderd-worker.1
	install -Dm 644 -t "$pkgdir/usr/share/man/man5" \
		contrib/docs/rebuilderd.conf.5 \
		contrib/docs/rebuilderd-sync.conf.5 \
		contrib/docs/rebuilderd-worker.conf.5
}

tools() {
	pkgdesc="$pkgdesc (tools)"
	install -Dm755 "$builddir/target/release/rebuildctl" -t "$subpkgdir/usr/bin"

	install -Dm644 "$builddir/completions/rebuildctl.bash" \
		"$subpkgdir/usr/share/bash-completion/completions/rebuildctl"
	install -Dm644 "$builddir/completions/_rebuildctl" \
		"$subpkgdir/usr/share/zsh/site-functions/_rebuildctl"
	install -Dm644 "$builddir/completions/rebuildctl.fish" \
		"$subpkgdir/usr/share/fish/vendor_completions.d/rebuildctl.fish"
}

sha512sums="
244e2161dc7cc3cee5ec73aa8a696fa78337f18b7d20060c7a3cf8b139897a032a35a444672ea6c3abd5b27c1715f45340e464a0f5affa4dbb5f4eaacf24e569  rebuilderd-0.19.0.tar.gz
14e9ff1526daaa3857c59ab92a60f5d9768851295db18d53a49c6aa122764fddaf659da342743cf1cf61b6c23b8387fd78f47ea657fbdb1936d37d9a8497aea2  rebuilderd.initd
b240f30d1a01addafb273199d917f7c42336ecb8b0fe6622d389e309b6c52b11f3859c7bb08df14738e39be357123df0a12796ff48ff06d43eda8dbad0a84977  rebuilderd-worker.initd
958119b17dcb477491694b0975b14691bf143ab19bc0079aebc55838861f95624060abf68409455267d824aaea8b7910ffd7762c3bf7077f001f40610a303ba9  rebuilderd.confd
85b02c4f0dfef17e55989ec34daf5dbe2027c781781db3e72b4f952ab2eae3043929d346ba37b1288bd8473d9fc29a2d19c1ddde26adc65190dd2fba73a02689  rebuilderd-sync.cron
a6ac8815466f0ae1d784042bd84bddb7aafc33e28f7f17735b65b341fa61a7282ad0280062c4d99dfbd0387a7d76aa10ba44b3569cccc2f084f5c6270678d8a0  getrandom-0.2.10.patch
61cde5d3b72d0efa183bbfe81d926bd96dabe9a17486b86108b72fd038e439bc1d402d1187a563fed6bae27c70e49243439a0a89dd3a0457e1724dd452defe84  shared-mime-info-2.3.patch
"
